package com.authine.cloudpivot.ext.model.yunshu;


import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通用附件处理类
 */
@Slf4j
@Data
@Accessors(chain = true)
@ApiModel("附件处理类")
public class Attachment implements Serializable {

    private static final long serialVersionUID = 881092630113733469L;

    private static final ObjectMapper mapper = new ObjectMapper();
    @ApiModelProperty("附件名称")
    private String name;
    @ApiModelProperty("附件url")
    private String url;
    @DateTimeFormat(pattern = DateUtils.FORMAT_PATTERN_DATETIME)
    @JsonFormat(pattern = DateUtils.FORMAT_PATTERN_DATETIME)
    @ApiModelProperty("附件上传时间")
    private Date time;


    /**
     * 文本转附件
     * 时间可以为空
     * @param text 格式要求：
     *             [{"name":"欧姆龙.jpg","url":"http://static.gomro.cn/cloud/inquiry/0045b98e-b8d6-48c6-a77c-250ac445b537.jpg","time":"2020-09-22 00:00:00"}]
     */
    public static List<Attachment> textToAttachment(String text) {
        List<Attachment> attachments = new ArrayList<>();
        try {
            if (RegexUtils.notNull(text)) {
                attachments = mapper.readValue(text, new TypeReference<List<Attachment>>() {
                });
            }
        } catch (Exception e) {
            log.error("附件：[{}] 转换失败，请检查！", text);
//            GomroExceptionUtils.error(e);
        }
        return attachments;
    }
}
